﻿/*
 * 由SharpDevelop创建。
 * 用户： Administrator
 * 日期: 2024/11/30
 * 时间: 1:11
 * 
 * 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
 */
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices; 

namespace Blur
{
	/// <summary>
	/// Description of MainForm.
	/// </summary>
	public partial class MainForm : Form
	{
		public MainForm()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
        //设置窗口透明，不透明度205
        SetWindowTransparent(this.Handle,205);
        //设置窗口模糊
        SetWindowAero(this.Handle);
        
		//	EnableBlur();
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
		}
		
		
        public const int GWL_EXSTYLE = -20;
        public const int LWA_ALPHA = 0x00000002;
        public const int WS_EX_LAYERED = 0x00080000;
        [DllImport("user32.dll")]
        public static extern int GetWindowLong(IntPtr hWnd, int nIndex);
        [DllImport("user32")]
        public static extern int SetWindowLong(IntPtr hwnd, int nIndex, int dwNewLong);

        [StructLayout(LayoutKind.Sequential)]
        public struct WindowCompositionAttributeData
        {
            public WindowCompositionAttribute Attribute;
            public IntPtr Data;
            public int SizeOfData;
        }
        public enum WindowCompositionAttribute
        {
            WCA_UNDEFINED = 0,
            WCA_NCRENDERING_ENABLED = 1,
            WCA_NCRENDERING_POLICY = 2,
            WCA_TRANSITIONS_FORCEDISABLED = 3,
            WCA_ALLOW_NCPAINT = 4,
            WCA_CAPTION_BUTTON_BOUNDS = 5,
            WCA_NONCLIENT_RTL_LAYOUT = 6,
            WCA_FORCE_ICONIC_REPRESENTATION = 7,
            WCA_EXTENDED_FRAME_BOUNDS = 8,
            WCA_HAS_ICONIC_BITMAP = 9,
            WCA_THEME_ATTRIBUTES = 10,
            WCA_NCRENDERING_EXILED = 11,
            WCA_NCADORNMENTINFO = 12,
            WCA_EXCLUDED_FROM_LIVEPREVIEW = 13,
            WCA_VIDEO_OVERLAY_ACTIVE = 14,
            WCA_FORCE_ACTIVEWINDOW_APPEARANCE = 15,
            WCA_DISALLOW_PEEK = 16,
            WCA_CLOAK = 17,
            WCA_CLOAKED = 18,
            WCA_ACCENT_POLICY = 19,
            WCA_FREEZE_REPRESENTATION = 20,
            WCA_EVER_UNCLOAKED = 21,
            WCA_VISUAL_OWNER = 22,
            WCA_LAST = 23
        }
        public enum ACCENT_STATE
        {
            ACCENT_DISABLED = 0,
            ACCENT_ENABLE_GRADIENT = 1,
            ACCENT_ENABLE_TRANSPARENTGRADIENT = 2,
            ACCENT_ENABLE_BLURBEHIND = 3,
            ACCENT_ENABLE_ACRYLICBLURBEHIND = 4,
            ACCENT_INVALID_STATE = 5
        }
        [StructLayout(LayoutKind.Sequential)]
        public struct ACCENT_POLICY
        {
            public ACCENT_STATE AccentState;
            public int AccentFlags;
            public int GradientColor;
            public int AnimationId;
        }
        [DllImport("user32.dll")]
        public static extern int SetWindowCompositionAttribute(IntPtr hwnd, ref WindowCompositionAttributeData data);

        [DllImport("user32.dll")]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        
        //设置分层窗口的不透明度和透明度颜色键
        [DllImport("user32", EntryPoint = "SetLayeredWindowAttributes")]
        private static extern int SetLayeredWindowAttributes(IntPtr hwnd,int crKey,int bAlpha,int dwFlags
        );
        
                //设置窗口透明效果
        private void SetWindowTransparent(IntPtr hwnd,int Alpha)
        {
            //IntPtr hwnd = FindWindow(null,"...");
            int OldStyle = GetWindowLong(hwnd,GWL_EXSTYLE);
            SetWindowLong(hwnd,GWL_EXSTYLE, OldStyle | WS_EX_LAYERED);
            SetLayeredWindowAttributes(hwnd, Color.FromArgb(200, Color.Gray).ToArgb(), Alpha,LWA_ALPHA);
        }
        
        
                //设置窗口模糊效果
        private void SetWindowAero(IntPtr hwnd)
        {
                //IntPtr hWnd = FindWindow(null,"...");
                ACCENT_POLICY accent = new ACCENT_POLICY
                {
                    AccentState = ACCENT_STATE.ACCENT_ENABLE_BLURBEHIND,
                    AccentFlags = 0,
                    AnimationId = 0,
                    GradientColor = 0
                };
                var accentStructSize = Marshal.SizeOf(accent);
                var accentPtr = Marshal.AllocHGlobal(accentStructSize);
                Marshal.StructureToPtr(accent, accentPtr, false);
                WindowCompositionAttributeData data = new WindowCompositionAttributeData
                {
                    Attribute = WindowCompositionAttribute.WCA_ACCENT_POLICY,
                    Data = accentPtr,
                    SizeOfData = accentStructSize
                };
                SetWindowCompositionAttribute(hwnd, ref data);
                Marshal.FreeHGlobal(accentPtr);   
        }


//
//		
//		
//		[DllImport("user32.dll")]
//        internal static extern int SetWindowCompositionAttribute(IntPtr hwnd, ref WindowCompositionAttributeData data);
// 
//        [StructLayout(LayoutKind.Sequential)]
//        internal struct WindowCompositionAttributeData
//        {
//            public WindowCompositionAttribute Attribute;
//            public IntPtr Data;
//            public int SizeOfData;
//        }
// 
//        internal enum WindowCompositionAttribute
//        {
//            WCA_ACCENT_POLICY = 19
//        }
// 
//        internal enum AccentState
//        {
//            ACCENT_DISABLED = 0,
//            ACCENT_ENABLE_GRADIENT = 1,
//            ACCENT_ENABLE_TRANSPARENTGRADIENT = 2,
//            ACCENT_ENABLE_BLURBEHIND = 3,
//            ACCENT_INVALID_STATE = 4
//        }
// 
//        [StructLayout(LayoutKind.Sequential)]
//        internal struct AccentPolicy
//        {
//            public AccentState AccentState;
//            public int AccentFlags;
//            public int GradientColor;
//            public int AnimationId;
//        }
// 
//        internal void EnableBlur()
//        {
//            var accent = new AccentPolicy();
//            var accentStructSize = Marshal.SizeOf(accent);
//            accent.AccentState = AccentState.ACCENT_ENABLE_BLURBEHIND;
// 
//            var accentPtr = Marshal.AllocHGlobal(accentStructSize);
//            Marshal.StructureToPtr(accent, accentPtr, false);
// 
//            var data = new WindowCompositionAttributeData();
//            data.Attribute = WindowCompositionAttribute.WCA_ACCENT_POLICY;
//            data.SizeOfData = accentStructSize;
//            data.Data = accentPtr;
// 
//            SetWindowCompositionAttribute(this.Handle, ref data);
// 
//            Marshal.FreeHGlobal(accentPtr);
//        }
	}
}
